/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.text.index.structures;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class InvertedIndexIntToStr {

    private Map<Integer, Set<String>> index;
    private List<Integer> sortedKeys;

    /**
     * 
     */
    public InvertedIndexIntToStr() {
        index = new HashMap<Integer, Set<String>>();
    }

    /**
     * 
     * @param key
     * @param element
     */
    public void addElement(Integer key, String element) {
        if (index.containsKey(key)) {
            index.get(key).add(element);
        } else {
            Set<String> tempSet = new HashSet<String>();
            tempSet.add(element);
            index.put(key, tempSet);
        }
    }

    /**
     * 
     * @return
     */
    public List<Integer> getSortedKeyList() {
        if (sortedKeys != null && sortedKeys.size() == index.keySet().size()) {
            return sortedKeys;
        } else {
            sortedKeys = new ArrayList<Integer>();
            Set<Integer> keySet = index.keySet();
            for (Integer key : keySet) {
                sortedKeys.add(key);
            }
            Collections.sort(sortedKeys);
            return sortedKeys;
        }

    }

    /**
     * 
     * @param integer
     * @return
     */
    public boolean containsKey(Integer integer) {
        return index.containsKey(integer);
    }

    /**
     * 
     * @param key
     * @return
     */
    public Set<String> getElement(Integer key) {
        return index.get(key);
    }

    /**
     * 
     * @return
     */
    public Map<Integer, Set<String>> getIndex() {
        return index;
    }

    /**
     * 
     * @param index
     */
    public void setIndex(Map<Integer, Set<String>> index) {
        this.index = index;
    }

    /**
     * 
     */
    public void printIndexCardinallity() {
        for (Integer key : index.keySet()) {
            System.out.println("Key: " + key + " Size: " + index.get(key).size());
        }
    }

    /**
     * 
     * @param aboveLimit
     */
    public void pruneAbove(int aboveLimit) {
        for (Iterator<Integer> it = index.keySet().iterator(); it.hasNext();) {
            if (index.get(it.next()).size() > aboveLimit) {
                it.remove();
            }
        }

    }

    /**
     * 
     * @param bellowLimit
     */
    public void pruneBellow(int bellowLimit) {
        for (Iterator<Integer> it = index.keySet().iterator(); it.hasNext();) {
            if (index.get(it.next()).size() < bellowLimit) {
                it.remove();
            }
        }
    }
    
    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer idx : index.keySet()) {
            stringBuilder.append(idx).append(" : ").append(index.get(idx).size()).append("\n");
        }
        return stringBuilder.toString();
    }
}
